package service.impl;

import models.Commont;
import play.db.jpa.JPA;
import service.CommontInterface;

import javax.persistence.Query;
import java.util.Date;
import java.util.List;

public class CommontImpl implements CommontInterface {
    @Override
    public List<Commont> getCommontListByPage(Integer contentId, Integer commontType, Integer pageSize, Integer pageNum) {
        StringBuffer sb = new StringBuffer();
        sb.append("from Commont c where c.contentId=:contentid ");
        if(commontType!=null&& commontType>0){
            sb.append(" and c.type=:type ");
        }
        Query query = JPA.em().createQuery(sb.toString(), Commont.class);
        query.setParameter("contentid", contentId);

        if(commontType!=null && commontType>0){
            query.setParameter("type", commontType);
        }
        query.setFirstResult((pageNum - 1) * pageSize);
        query.setMaxResults(pageSize);
        return query.getResultList();
    }

    @Override
    public void createCommont(Commont commont) {
        commont.create_time=new Date();
        commont.save();
    }
}
